using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Silvery
{
    public class NSpirit : NImageD, ISpirit
    {
        public EmSpiritDataMode SpiritDataMode { get;private set; }
        public EmAnimatMode AnimatMode { get; set; }
        public NSpirit()
        {
            AnimatMode = EmAnimatMode.Loop;
            base.IsSelfLoopRun = true;
            Content.RenderTransformOrigin = new Point(0.5, 0.5);
        }
        ISpiritData spiritData;
        /// <summary>
        /// 设置或获取资源文件，设置之后需要重设动画名字，否则会一直播放前面的动画
        /// </summary>
        public ISpiritData SpiritData
        {
            get { return spiritData; }
            set
            {
                if (spiritData == value)
                    return;
                spiritData = value;
                base.SelfLoopInterval = TimeSpan.FromMilliseconds(value.FramesInfo.Millisecond);
                if(animatname != "")
                    SetSpiritAnimatName(animatname);
            }
        }
        
        bool SetSpiritAnimatName(string name)
        {
            if (SpiritData != null)
            {
                var root = SpiritData.FindAnimat(name);
                if (root != null)
                {
                    AnimatSpirit = root;
                    if (CurrentFrameNumber >= AnimatSpirit.Count)
                        CurrentFrameNumber = 0;
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
            
        }
        
        /// <summary>
        /// 正在播放的动画列表
        /// </summary>
        public IList<Image> AnimatSpirit { get; private set; }
        
        int iframe = 0;
        /// <summary>
        /// 当前正播放的帧数
        /// </summary>
        public int CurrentFrameNumber
        {
            get { return iframe; }
            set
            {
                if (value < 0)
                    iframe = 0;
                else
                    iframe = value;
                if (AnimatFrameChanged != null)
                    AnimatFrameChanged(this);
            }
        }
        /// <summary>
        /// 当前最大的帧数总数
        /// </summary>
        public int CurrentFramesSum { get { return AnimatSpirit == null ? 0 : AnimatSpirit.Count; } }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="timer"></param>
        public override void OnUpdata(BaseObject sender, ITimer timer)
        {
            if (AnimatSpirit != null && CurrentAnimatState == AnimatState.Playing)
            {
                if (CurrentFrameNumber >= AnimatSpirit.Count)
                {
                    if (AnimatMode == EmAnimatMode.Once)
                        return;
                    CurrentFrameNumber = 0;
                }
                var img = AnimatSpirit[CurrentFrameNumber] as Image;
                base.Content.Width = (img.Source as BitmapImage).PixelWidth;
                base.Content.Height = (img.Source as BitmapImage).PixelHeight;
                base.Content.Source = img.Source;
                if (img.Tag != null)
                    this.OffsetPoint = (Point)img.Tag;
                base.Content.RenderTransform = img.RenderTransform;

                CurrentFrameNumber += 1;
            }
            base.OnUpdata(sender, timer);
        }
        string animatname = "";
        /// <summary>
        /// 动画名字
        /// </summary>
        public string CurrentAnimatName
        {
            get { return animatname; }
            set
            {
                if (value == animatname)
                    return;
                if (SetSpiritAnimatName(value) || animatname == "")
                    animatname = value;
            }
        }
        /// <summary>
        /// 动画播放状态
        /// </summary>
        public AnimatState CurrentAnimatState { set; get; }

        /// <summary>
        /// 精灵帧发生变化的时候的委托
        /// </summary>
        public event AnimatFrameChangedEventHandler AnimatFrameChanged;
        public override void Dispose()
        {
            
            AnimatSpirit = null;
            if (SpiritData != null)
                SpiritData.Dispose();
            SpiritData = null;
            AnimatFrameChanged = null;
            base.Dispose();
            
        }
    }
}
